package com.tzbank.wisdom.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tzbank.esb.util.EsbConsumerUtil;
import com.tzbank.redis.RedisUtils;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.DateUtils;
import com.tzbank.utils.ResultCode;
import com.tzbank.utils.ResultUtil;
import com.tzbank.wisdom.constant.SysConstans;
import com.tzbank.wisdom.service.BusinessService;
import com.tzbank.wisdom.service.CommonService;
import com.tzbank.wisdom.service.FinanceService;
import com.tzbank.wisdom.utils.BusinessCodeUtil;
import com.tzbank.wisdom.utils.RequestMap;
import com.tzbank.wisdom.utils.TransCodeEnum;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;



@Service(value = "FinanceService")
@Slf4j
public class FinanceServiceImpl implements FinanceService {
	@Autowired
    private BusinessService businessService;
	@Autowired
    private CommonService commonService;
	
	/**
	 * @param 获取公共参数
	 * @return
	 */
	public Map<String, Object> getInputMap(Map<String, String> reqBody) {

		// 组装入参Map
		Map<String, Object> inputMap = new HashMap<String, Object>();
		Map<String, Object> maps =  JSONObject.fromObject(reqBody);
		inputMap = RequestMap.getInputMap(maps);
		Map<String, Object> userMap = RequestMap.getInputUserInfo((String)reqBody.get("clientSeqNo"));
		if (userMap.get("clientNo")==null) {
			return userMap;
		}
		inputMap.putAll(userMap);
		return inputMap;
	}
	
	
	
	/**
	 * 
	 * 理财产品清单列表查询
	 * @param reqBody
	 * @return
	 */
	@Override
	public Map<String, Object> getFinanceAll(Map<String, String> reqBody) {
		log.info("理财产品清单列表查询请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		//调用核心计价因子报文头入参
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.FINANCE_PRODUCTALL.getServiceCode(), TransCodeEnum.FINANCE_PRODUCTALL.getServiceSence(), 
				"0110", SysConstans.ABP_CODE, "");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		//调用核心计价因子入参 
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("taCode", reqBody.get("taCode"));        //TA代码
		inputMap.put("productCode", reqBody.get("productCode")); //产品编号
		inputMap.put("productStatus", reqBody.get("productStatus"));        //产品状态
		inputMap.put("productRiskLevel", reqBody.get("productRiskLevel")); //产品风险等级
		inputMap.put("productManager", reqBody.get("productManager"));        //产品经理工号
		inputMap.put("prodProperty", reqBody.get("prodProperty")); //产品属性
		inputMap.put("queryType", reqBody.get("queryType"));        //查询类型
		inputMap.put("templateCode", reqBody.get("templateCode")); //模板编号
		inputMap.put("raiseStartDate", reqBody.get("raiseStartDate"));        //募资起始日期
		inputMap.put("raiseEndDate", reqBody.get("raiseEndDate")); //募资终止日期
		
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); // 理财产品清单列表查询结果
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
		    log.info("理财产品清单列表查询响应"+JSONObject.fromObject(result));
			return result;
		}
	   JSONObject dataJson = JSONObject.fromObject(result.get("data"));
		//返回的是全部产品列表
	    JSONArray productListJson = (JSONArray) dataJson.get("queryResultArray");	
	   //筛选需要字段
	    JSONArray productListJsonNew = new JSONArray();
	  
	    for(int i=0;i<productListJson.size();i++) {
	    	
	    	JSONObject productOne =new JSONObject();
	    	productOne.put("productCode", productListJson.getJSONObject(i).get("productCode"));  //产品编号
	    	productOne.put("productName", productListJson.getJSONObject(i).get("productName"));  //产品名称
	    	productOne.put("productType", productListJson.getJSONObject(i).get("productType"));  //产品类型
	    	productOne.put("productStatus", productListJson.getJSONObject(i).get("productStatus"));  //产品状态
	    	productOne.put("productStatusName", productListJson.getJSONObject(i).get("productStatusName")); //产品状态名称
	    	productOne.put("productRiskLevel", productListJson.getJSONObject(i).get("productRiskLevel")); //产品风险等级
	    	productOne.put("productRiskLevelName", productListJson.getJSONObject(i).get("productRiskLevelName")); //产品风险等级名称
	    	productOne.put("prodProperty", productListJson.getJSONObject(i).get("prodProperty")); //产品属性
	    	productOne.put("prodPropertyName", productListJson.getJSONObject(i).get("prodPropertyName")); //产品属性名称
	    	productOne.put("expectYieldS", productListJson.getJSONObject(i).get("expectYieldS")); //预期收益率(字符型的)
	    	productOne.put("cycleDays", productListJson.getJSONObject(i).get("cycleDays")); //周期天数
	    	productOne.put("pFirMinAmt", productListJson.getJSONObject(i).get("pFirMinAmt"));  //个人首次最低投资金额
	    	productOne.put("curAvlLmt", productListJson.getJSONObject(i).get("curAvlLmt")); //当前可用额度
	    	productOne.put("raiseAmtMax", productListJson.getJSONObject(i).get("raiseAmtMax"));  //最高募集金额
	    	/*productOne.put("templateCode", productListJson.getJSONObject(i).get("templateCode"));  //最高募集金额
	    	productOne.put("endDate", productListJson.getJSONObject(i).get("endDate"));  //最高募集金额*/
	    	//挡板数据 数据格式为科学计数法+++++++++++++++++++**********************************
	    	Double curAvlLmt =(Double)productListJson.getJSONObject(i).get("curAvlLmt");
	    	Double raiseAmtMax =(Double)productListJson.getJSONObject(i).get("raiseAmtMax");
	    	BigDecimal curAvlLmtB=new BigDecimal(curAvlLmt.toString());
	    	BigDecimal raiseAmtMaxB=new BigDecimal(raiseAmtMax.toString());
	    	productOne.put("curAvlLmt",curAvlLmtB.toPlainString());
	    	productOne.put("raiseAmtMax", raiseAmtMaxB.toPlainString());
	    	//挡板数据 数据格为科学计数法++++++++++++++++++++**********************************
	    	productListJsonNew.add(productOne);
	    }
		
		 dataJson.remove("queryResultArray");
		 dataJson.put("allFinanceArray",productListJsonNew);
		 //强烈推荐  和 定存 现在 写死 
		 JSONArray commentArray = new JSONArray();
		 JSONArray fixSaveArray = new JSONArray();
		 dataJson.put("commentArray",commentArray);
		 dataJson.put("fixSaveArray",fixSaveArray);
		 //  -------投资总额 需要从 我的理财列表去查(totalAmt)-----------------------------------------
		 Map<String, Object> map =this.buyFinanceList(reqBody);
		 if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
			    log.info("查询投资总额响应"+JSONObject.fromObject(map));
				return map;
		}
		JSONObject mapMyBuy = JSONObject.fromObject(map.get("data"));
		//返回的是全部产品列表
		dataJson.put("totalAmt",mapMyBuy.get("totalAmt"));
		dataJson.remove("head");
		 result.remove("data");
		 result.put("data", dataJson);	
		 log.info("理财产品清单列表查询响应"+JSONObject.fromObject(result));
		 return result;
		
	}
	
	/**
	 * 
	 * 理财-理财产品详情查询
	 * @param reqBody
	 * @return
	 */
	@Override
	public Map<String, Object> getFinanceDetail(Map<String, String> reqBody) {
 		log.info("理财产品详情查询请求参数【{}】", JSONObject.fromObject(reqBody).toString());
 		//调用核心计价因子入参 
 		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.FINANCE_PRODUCTDETAIL.getServiceCode(), TransCodeEnum.FINANCE_PRODUCTDETAIL.getServiceSence(), 
				"0110", SysConstans.ABP_CODE, "");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("taCode", reqBody.get("taCode"));        //TA代码
		inputMap.put("productCode", reqBody.get("productCode")); //产品编号
		inputMap.put("productStatus", reqBody.get("productStatus"));        //产品状态
		inputMap.put("productRiskLevel", reqBody.get("productRiskLevel")); //产品风险等级
		inputMap.put("productManager", reqBody.get("productManager"));        //产品经理工号
		inputMap.put("prodProperty", reqBody.get("prodProperty")); //产品属性
		inputMap.put("valueDate", reqBody.get("valueDate"));        //起息日期
		inputMap.put("matureDate", reqBody.get("matureDate")); //到期日期
		inputMap.put("queryType", reqBody.get("queryType"));        //查询类型
		inputMap.put("bidFlag", reqBody.get("bidFlag")); //招投标产品标志
		inputMap.put("raiseStartDate", reqBody.get("raiseStartDate"));        //募资起始日期
		inputMap.put("raiseEndDate", reqBody.get("raiseEndDate")); //募资终止日期
		inputMap.put("investStartDate", reqBody.get("investStartDate"));        //投资起始日期
		inputMap.put("investEndDate", reqBody.get("investEndDate")); //投资终止日期
		inputMap.put("acctNo", reqBody.get("acctNo"));        //账号
		inputMap.put("templateCode", reqBody.get("templateCode")); //模板编号
		inputMap.put("flag", reqBody.get("flag"));        //标志
		inputMap.put("bankId", reqBody.get("bankId")); //银行代码
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); 
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
		    log.info("理财产品详情查询响应"+JSONObject.fromObject(result));
			return result;
		}
		JSONObject dataJson = JSONObject.fromObject(result.get("data"));
		//返回的是全部产品列表
		JSONArray productListJson = (JSONArray) dataJson.get("queryResultArray");	
		//筛选需要字段
		JSONArray productListJsonNew = new JSONArray();
		for(int i=0;i<productListJson.size();i++) {
	    	JSONObject productOne =new JSONObject();
	    	productOne.put("curAvlLmt", reqBody.get("curAvlLmt"));  //当前可用额度
	    	productOne.put("productCode", productListJson.getJSONObject(i).get("productCode"));  //产品编号
	    	productOne.put("productName", productListJson.getJSONObject(i).get("productName"));  //产品名称
	    	productOne.put("expectYieldS", productListJson.getJSONObject(i).get("expectYieldS")); //预期收益率(字符型的)
	    	productOne.put("incomeTypeNo", productListJson.getJSONObject(i).get("incomeTypeNo"));
	    	productOne.put("incomeTypeName", productListJson.getJSONObject(i).get("incomeTypeName"));  
	    	productOne.put("ccy", productListJson.getJSONObject(i).get("ccy")); 
	    	productOne.put("cycleDays", productListJson.getJSONObject(i).get("cycleDays")); 
	    	productOne.put("pFirMinAmt", productListJson.getJSONObject(i).get("pFirMinAmt"));
	    	productOne.put("pMinAppAmt", productListJson.getJSONObject(i).get("pMinAppAmt")); 
	    	productOne.put("pMinHoldLot", productListJson.getJSONObject(i).get("pMinHoldLot")); 
	    	productOne.put("productRiskLevel", productListJson.getJSONObject(i).get("productRiskLevel")); 
	    	productOne.put("productRiskLevelName", productListJson.getJSONObject(i).get("productRiskLevelName"));
	    	String startDate=DateUtils.getdatefor_yyyy_MM_dd(productListJson.getJSONObject(i).getString("startDate"));
	    	String endDate=DateUtils.getdatefor_yyyy_MM_dd(productListJson.getJSONObject(i).getString("endDate"));
	    	String valueDate=DateUtils.getdatefor_yyyy_MM_dd(productListJson.getJSONObject(i).getString("valueDate"));
	    	String matureDate=DateUtils.getdatefor_yyyy_MM_dd(productListJson.getJSONObject(i).getString("matureDate"));
	    	productOne.put("startDate", startDate);  
	    	productOne.put("endDate",endDate); 
	    	productOne.put("valueDate", valueDate);  
	    	productOne.put("matureDate", matureDate);
	    	//当前日期
			String today ="";
			Date dt =new Date();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
			today=sdf.format(dt);
	    	//当前日期做比较 判断 时间轴处于什么状态   0 募集开始 1 募集结束 2 起息日  3 到期日（timeStatues）   ***************时间轴状态 待确认
	    	if(DateUtils.getQuotByDays(matureDate,today)>=0) {
	    		productOne.put("timeStatues", "3");
	    	}else if(DateUtils.getQuotByDays(valueDate,today)>=0) {
	    		productOne.put("timeStatues", "2");
	    	}else if(DateUtils.getQuotByDays(endDate,today)>=0) {
	    		productOne.put("timeStatues", "1");
	    	}else {
	    		productOne.put("timeStatues", "0");
	    	}
	    	
	    	productListJsonNew.add(productOne);
	    }
		dataJson.remove("queryResultArray");
		dataJson.put("queryResultArray",productListJsonNew);
		dataJson.remove("head");
		result.remove("data");
		result.put("data", dataJson);
		log.info("理财产品详情查询响应"+JSONObject.fromObject(result));
		return result;
	}
    
	
	
	//  statusFlag 0 赎回 1 赎回取消 2 认购取消  3投资中  4 赎回中  5不显示按钮               clickFlag   0 可点击  1 不可点击
	@Override
	public Map<String, Object> buyFinanceList(Map<String, String> reqBody) {
		log.info("客户购买的理财产品列表请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		//调用核心计价因子入参 
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.FINANCE_BUYPRODUCTLIST.getServiceCode(), TransCodeEnum.FINANCE_BUYPRODUCTLIST.getServiceSence(), 
				"0110", SysConstans.ABP_CODE, "");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		inputMap.put("sysHead", reqSysHead);
		
		inputMap.put("closedAssetStatus", "0"); //查询全部
		inputMap.put("openAssetStatus", "0"); 
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); 
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
		    log.info("客户购买的理财产品列表响应"+JSONObject.fromObject(result));
			return result;
		}
		JSONObject dataJson = JSONObject.fromObject(result.get("data"));
		//返回购买的全部产品列表
	    JSONArray productListJson = (JSONArray) dataJson.get("queryResultArray");	
	    //所有产品的集合
	    JSONArray allInList = new JSONArray();
	    //稳进账list
	    JSONArray steadyInList = new JSONArray();
	    //周周盈list
	    JSONArray weekInList = new JSONArray();
	    //日日盈list
	    JSONArray dayInList = new JSONArray();
	    Double totalAmt=0.00;
		 for(int i=0;i<productListJson.size();i++) {
		    	JSONObject productOne =new JSONObject();
		    	productOne.put("productCode", productListJson.getJSONObject(i).get("productCode"));  
		    	productOne.put("productName", productListJson.getJSONObject(i).get("productName"));  
		    
		    	productOne.put("expectYield", productListJson.getJSONObject(i).get("expectYield"));
		    	productOne.put("acctNo", productListJson.getJSONObject(i).get("acctNo")); 
		    	productOne.put("oriTranSeqNo", productListJson.getJSONObject(i).get("oriTranSeqNo")); 
		    	//productOne.put("oriTranDate", productListJson.getJSONObject(i).get("oriTranDate")); 
		    	String assetType =productListJson.getJSONObject(i).getString("assetType");  // 0稳进账 1 周周盈 2 日日盈
		    	String buyFlag =productListJson.getJSONObject(i).getString("buyFlag");  
		    	Double availLot  = (Double)productListJson.getJSONObject(i).get("availLot");
		    	Double amt  = (Double)productListJson.getJSONObject(i).get("amt");
		    	Double lotDouble  = (Double)productListJson.getJSONObject(i).get("lot");
		    	String setupDate =productListJson.getJSONObject(i).getString("setupDate"); 
		    	String oriTranDate =productListJson.getJSONObject(i).getString("oriTranDate"); 
		    	String changeDate =productListJson.getJSONObject(i).getString("changeDate"); 
		    	//投资金额
		    	Double lot=0.00;
		    	//投资日期
		    	String startDate ="";
		    	
		    	if(assetType.equals("0")) {
		    		if(availLot==0.00) {
		    			lot=amt;
		    		}else {
		    			lot=availLot;
		    		}
		    		//投资金额
		    		productOne.put("lot", lot);
		    		//投资日期
		    		productOne.put("startDate", DateUtils.getdatefor_yyyy_MM_dd(setupDate));
		    		if(buyFlag.equals("0")) {
		    			//产品状态 
		    			productOne.put("statusFlag", "2");
		    			//是否可点击
		    			productOne.put("clickFlag", "0");
		    		}else if(buyFlag.equals("00")) {
		    			productOne.put("statusFlag", "3");
		    			productOne.put("clickFlag", "1");
		    		}
		    		steadyInList.add(productOne);
		    	}else if(assetType.equals("1")){
		    		if(availLot==0.00) {
		    			if(buyFlag.equals("3")||buyFlag.equals("33")) {
		    				lot=lotDouble;
		    			}else {
		    				lot=amt;
		    			}
		    		}else {
		    			lot=availLot;
		    		}
		    		//投资金额
		    		productOne.put("lot", lot);
		    		
		    		if(buyFlag.equals("1")) {
		    			startDate=changeDate;
		    		}else {
		    			startDate=oriTranDate;
		    		}
		    		//投资日期
		    		productOne.put("startDate",  DateUtils.getdatefor_yyyy_MM_dd(startDate));
		    		
		    		if(buyFlag.equals("0")) {
		    			productOne.put("statusFlag", "2");
		    			productOne.put("clickFlag", "0");
		    		}else if(buyFlag.equals("1")&&availLot!=0.00) {
		    			productOne.put("statusFlag", "0");
		    			productOne.put("clickFlag", "0");
		    		}else if(buyFlag.equals("1")&&availLot==0.00) {
		    			productOne.put("statusFlag", "5");
		    			productOne.put("clickFlag", "1");
		    		}else if(buyFlag.equals("3")) {
		    			productOne.put("statusFlag", "1");
		    			productOne.put("clickFlag", "0");
		    		}else if(buyFlag.equals("00")) {
		    			productOne.put("statusFlag", "2");
		    			productOne.put("clickFlag", "1");
		    		}else if(buyFlag.equals("33")) {
		    			productOne.put("statusFlag", "4");
		    			productOne.put("clickFlag", "1");
		    		}
		    		
		    		weekInList.add(productOne);
		    	}else if(assetType.equals("2")){
		    		if(availLot==0.00) {
		    			if(buyFlag.equals("3")||buyFlag.equals("33")) {
		    				lot=lotDouble;
		    			}else {
		    				lot=amt;
		    			}
		    		}else {
		    			lot=availLot;
		    		}
		    		//投资金额
		    		productOne.put("lot", lot);
		    		if(buyFlag.equals("1")) {
		    			startDate=changeDate;
		    		}else {
		    			startDate=oriTranDate;
		    		}
		    		//投资日期
		    		productOne.put("startDate", DateUtils.getdatefor_yyyy_MM_dd(startDate));
		    		
		    		if(buyFlag.equals("0")) {
		    			productOne.put("statusFlag", "2");
		    			productOne.put("clickFlag", "0");
		    		}else if(buyFlag.equals("1")&&availLot!=0.00) {
		    			productOne.put("statusFlag", "0");
		    			productOne.put("clickFlag", "0");
		    		}else if(buyFlag.equals("1")&&availLot==0.00) {
		    			productOne.put("statusFlag", "5");
		    			productOne.put("clickFlag", "1");
		    		}else if(buyFlag.equals("3")) {
		    			productOne.put("statusFlag", "1");
		    			productOne.put("clickFlag", "0");
		    		}else if(buyFlag.equals("00")) {
		    			productOne.put("statusFlag", "2");
		    			productOne.put("clickFlag", "1");
		    		}else if(buyFlag.equals("33")) {
		    			productOne.put("statusFlag", "4");
		    			productOne.put("clickFlag", "1");
		    		}
		    		dayInList.add(productOne);
		    	}
		    	
		    	//投资总额totalAmt
		    	totalAmt+=lot;
		    	
		    	
		    }
			 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++测试数据 记得删除++++++++
			 steadyInList.getJSONObject(0).put("statusFlag", "0");
			 steadyInList.getJSONObject(0).put("clickFlag", "0");
			 
			 steadyInList.getJSONObject(1).put("statusFlag", "1");
			 steadyInList.getJSONObject(1).put("clickFlag", "0");
			 
			 steadyInList.getJSONObject(2).put("statusFlag", "2");
			 steadyInList.getJSONObject(2).put("clickFlag", "0");
			//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			 JSONObject steadyInListJson =new JSONObject();
			 steadyInListJson.put("name", "稳进账");
			 steadyInListJson.put("ename", "steadyIn");
			 steadyInListJson.put("listOne", steadyInList);
			 JSONObject weekInListJson =new JSONObject();
			 weekInListJson.put("name", "周周盈");
			 weekInListJson.put("ename", "weekIn");
			 weekInListJson.put("listOne", weekInList);
			 JSONObject dayInListJson =new JSONObject();
			 dayInListJson.put("name", "日日盈");
			 dayInListJson.put("ename", "dayIn");
			 dayInListJson.put("listOne", dayInList);
			 allInList.add(steadyInListJson);
			 allInList.add(weekInListJson);
			 allInList.add(dayInListJson);
			 dataJson.remove("queryResultArray");
			 dataJson.remove("head");
			 dataJson.put("allInList", allInList);
			 dataJson.put("totalAmt", totalAmt);
			 result.remove("data");
			 result.put("data", dataJson);
		
			 log.info("客户购买的理财产品列表响应"+JSONObject.fromObject(result));	
			 return result;
	}
	
	
	
	
	
	/**
	 * 理财产品购买
	 * 
	 * @param reqBody
	 * @return
	 */
	@Override
	public Map<String, Object> buy(Map<String, String> reqBody) {
		log.info("理财产品购买请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		Map<String, Object> mappwd = commonService.verifyTransPassword(reqBody);
		if (!CodeUtil.CODE_200.getCode().equals(mappwd.get("code"))) {
			mappwd.put("code", CodeUtil.CODE_10021.getCode());
			mappwd.put("msg", CodeUtil.CODE_10021.getName());
			log.info("校验交易密码 响应结果:{}", mappwd.get("msg"));
			return mappwd;
		}
		//查询是否签约理财
		Map<String, Object> querySign=querySign(reqBody);
		if (!CodeUtil.CODE_200.getCode().equals(querySign.get("code"))) {
			log.info("查询签约响应结果:{}", querySign.get("msg"));
			return querySign;
		}
		JSONObject querySignJson = JSONObject.fromObject(querySign.get("data"));
		JSONArray querySignList = (JSONArray) querySignJson.get("queryResultArray");
		//判断客户是否签约 没签约 自动签约  signStatus  0-签约  	1-解约
		if(querySignJson!=null) {
			if(querySignList.getJSONObject(0).getString("signStatus").equals("1")) {  //解约 调用签约接口
				//理财签约接口
				Map<String, Object> sign= sign(reqBody);
				if (!CodeUtil.CODE_200.getCode().equals(sign.get("code"))) {
					log.info("理财签约响应结果:{}", sign.get("msg"));
					return sign;
				}
			}
		}
		//调用核心计价因子入参 
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.FINANCE_BUYFINANCE.getServiceCode(), TransCodeEnum.FINANCE_BUYFINANCE.getServiceSence(), 
				"0110", SysConstans.ABP_CODE, "");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("acctNo", reqBody.get("acctNo"));        //	账号    6224271190391118-101
		inputMap.put("subsAmt", reqBody.get("subsAmt")); //认购金额
		inputMap.put("productCode", reqBody.get("productCode"));//产品编号   RRY008
		inputMap.put("certNo", UUID.randomUUID().toString()); //凭证号
		inputMap.put("openCardBranch", "14");//开卡机构	14
		
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); // 发送短信动态码结果
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
		    log.info("理财产品购买响应"+JSONObject.fromObject(result));
			return result;
		}
		JSONObject dataJson = JSONObject.fromObject(result.get("data"));
		dataJson.remove("head");
		result.put("data", dataJson);
		log.info("理财产品购买响应"+JSONObject.fromObject(result));
		return result;
	}
    
	/**
	 * 认购取消 或者赎回取消      
	 * 
	 * @param reqBody
	 * @return
	 */
	@Override
	public Map<String, Object> cancelBuy(Map<String, String> reqBody) {
		
		log.info("认购取消 或者赎回取消  请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		Map<String, Object> mappwd = commonService.verifyTransPassword(reqBody);
		if (!CodeUtil.CODE_200.getCode().equals(mappwd.get("code"))) {
			mappwd.put("code", CodeUtil.CODE_10021.getCode());
			mappwd.put("msg", CodeUtil.CODE_10021.getName());
			log.info("校验交易密码 响应结果:{}", mappwd.get("msg"));
			return mappwd;
		}
		//调用核心计价因子入参 
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.FINANCE_CANCELBUY.getServiceCode(), TransCodeEnum.FINANCE_CANCELBUY.getServiceSence(), 
				"0110", SysConstans.ABP_CODE, "");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		inputMap.put("sysHead", reqSysHead);
		
		inputMap.put("tranType",reqBody.get("tranType"));        //交易类型   1-认购取消   	2-赎回取消            "1"
		inputMap.put("oriTranSeqNo", reqBody.get("oriTranSeqNo")); //原交易流水号
		inputMap.put("tranAmt", reqBody.get("tranAmt")); //交易金额
		inputMap.put("acctNo", reqBody.get("acctNo")); //账号
		
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); // 发送短信动态码结果
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
		    log.info("认购取消 或者赎回取消响应"+JSONObject.fromObject(result));
			return result;
		}
		 JSONObject dataJson = JSONObject.fromObject(result.get("data"));
		 dataJson.remove("head");
		 result.put("data", dataJson);
		log.info("认购取消 或者赎回取消响应"+JSONObject.fromObject(result));
		return result;
	}
    
	/**
	 * 赎回
	 * 
	 * @param reqBody
	 * @return
	 */
	@Override
	public Map<String, Object> redeem(Map<String, String> reqBody) {
		log.info("赎回请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		Map<String, Object> mappwd = commonService.verifyTransPassword(reqBody);
		if (!CodeUtil.CODE_200.getCode().equals(mappwd.get("code"))) {
			mappwd.put("code", CodeUtil.CODE_10021.getCode());
			mappwd.put("msg", CodeUtil.CODE_10021.getName());
			log.info("校验交易密码 响应结果:{}", mappwd.get("msg"));
			return mappwd;
		}
		//调用核心计价因子报文头入参
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.FINANCE_REDOM.getServiceCode(), TransCodeEnum.FINANCE_REDOM.getServiceSence(), 
				"0110", SysConstans.ABP_CODE, "");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("lot", reqBody.get("lot"));        //份额
		inputMap.put("tranAmt", reqBody.get("tranAmt"));        //金额 和 份额同值
		inputMap.put("productCode", reqBody.get("productCode")); //产品编号
		inputMap.put("acctNo", reqBody.get("acctNo")); //账号
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); // 赎回结果
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
		    log.info("赎回响应"+JSONObject.fromObject(result));
			return result;
		}
		JSONObject dataJson = JSONObject.fromObject(result.get("data"));
		dataJson.remove("head");
		result.put("data", dataJson);
		log.info("赎回响应"+JSONObject.fromObject(result));
		return result;
	}
    
	
	/**
	 * 客户持有理财产品查询(05003000011)
	 * 
	 * @param reqBody
	 * @return
	 */
	@Override
	public Map<String, Object> myProductAll(Map<String, String> reqBody) {
		log.info("请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		//调用核心计价因子报文头入参
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.FINANCE_ALL_MY.getServiceCode(), TransCodeEnum.FINANCE_ALL_MY.getServiceSence(), 
				"0110", SysConstans.ABP_CODE, "");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		inputMap.put("sysHead", reqSysHead);
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); // 
		// 当ESB数据返回成功的时候进行数据处理。
		if (CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
			Map<String, Object> data = (Map<String, Object>) result.get("data");
			List listArray = (List) data.get("queryResultArray");					
			List<Object> realList = new ArrayList<Object>();
			for (int i = 0; i < listArray.size(); i++) {
				Map<String, Object> realDate = new HashMap<String, Object>();
				Map<String, Object> params = (Map<String, Object>) listArray.get(i);					
				realDate.put("productCode", (String) params.get("productCode"));// 产品名称
				realDate.put("productName", (String) params.get("productName"));// 产品名称
				realDate.put("totPrdValue", (double) params.get("totPrdValue"));// 产品总市值
				realDate.put("acctNo", (String) params.get("acctNo"));//份额所属帐号
				realDate.put("convFlag", (String) params.get("convFlag"));//0-可转入,可转出1-只可转入2-只可转出3-不可转换	
				realDate.put("productStatus", (String) params.get("productStatus"));// 产品状态0-开放期1-募集期2-发行成功3-发行失败4-停止交易5-停止申购6-停止赎回7-权益登记8-红利发放9-产品封闭期a-产品终止b-预约认购期
				realList.add(realDate);
			}
			data.remove("head");
			data.remove("queryResultArray");
			data.put("FinanceResultArray", realList);
			result.remove("data");
			result.put("data", data);
			log.info("响应信息：[{}]", JSONObject.fromObject(result).toString());
		} else {
			log.info("ESB报文数据返回出错！");
			return ResultUtil.resultMap(BusinessCodeUtil.CODE_40001.getName(), BusinessCodeUtil.CODE_40001.getCode(),
					result.get("msg"));
		}	
		return result;
	}

	
	/**
	 * 查询客户风险等级  客户是否需要进行风险评估 （levelFlag 0 未进行风险评估  1 进行了风险评估且没有过期  2进行了风险评估但是已经过期 ）
	 * 
	 * @param reqBody
	 * @return
	 */
	@Override
	public Map<String, Object> getUserLevel(Map<String, String> reqBody) {
		log.info("查询客户风险等级请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		//调用核心计价因子报文头入参
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.FINANCE_USERLEVEL.getServiceCode(), TransCodeEnum.FINANCE_USERLEVEL.getServiceSence(), 
				"", SysConstans.ABP_CODE, "108030");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("clientType", "1");
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); //客户风险等级查询结果
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
		    log.info("查询客户风险等级响应"+JSONObject.fromObject(result));
			return result;
		}
		JSONObject dataJson = JSONObject.fromObject(result.get("data"));
		dataJson.remove("head");
		result.put("data", dataJson);
		//是否需要进行风险评估（levelFlag 0 未进行风险评估  1 进行了风险评估且没有过期  2进行了风险评估但是已经过期）
		String levelFlag ="0";
	    String ineffectDate=DateUtils.getdatefor_yyyy_MM_dd(dataJson.getString("ineffectDate"));
	    //当前日期
		String today ="";
		Date dt =new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
		today=sdf.format(dt);
		if(dataJson!=null) {
			if(StringUtils.isNotBlank(dataJson.getString("riskCode"))) {
				if(DateUtils.getQuotByDays(today,ineffectDate)>=0) {
					levelFlag="1";
				}else {
					levelFlag="2";
				}
				
			}
		}
		dataJson.put("levelFlag", levelFlag);
		log.info("查询客户风险等级响应"+JSONObject.fromObject(result));
		
		return result;
	}
	
	
	
	/**
	 * 客户风险等级确认   
	 * 
	 * @param reqBody
	 * @return
	 */
	@Override
	public Map<String, Object> levelConfirm(Map<String, String> reqBody) {
		log.info("客户风险等级确认请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		//调用核心计价因子报文头入参
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.FINANCE_LEVELCONFIRM.getServiceCode(), TransCodeEnum.FINANCE_LEVELCONFIRM.getServiceSence(), 
				"", SysConstans.ABP_CODE, "108030");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("lastScore", reqBody.get("lastScore"));
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); //客户风险等级确认结果
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
		    log.info("客户风险等级确认响应"+JSONObject.fromObject(result));
			return result;
		}
		 JSONObject dataJson = JSONObject.fromObject(result.get("data"));
		 dataJson.remove("head");
		 result.put("data", dataJson);
		log.info("客户风险等级确认响应"+JSONObject.fromObject(result));
		
		return result;
	}
	
	/**
	 * 理财签约接口    入参签约账号    acctNo 也是购买时候选择的活期账号
	 * 
	 * @param reqBody
	 * @return
	 */
	@Override
	public Map<String, Object> sign(Map<String, String> reqBody) {
		log.info("理财签约接口请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		//调用核心计价因子报文头入参
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.FINANCE_SIGN.getServiceCode(), TransCodeEnum.FINANCE_SIGN.getServiceSence(), 
				"", SysConstans.ABP_CODE, "108030");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		inputMap.put("sysHead", reqSysHead);
		//从Redis中获取  证件类型  证件号码   客户名称
		Map<?, ?> userMap = RedisUtils.getUserInfo(reqBody.get("clientSeqNo"));
        if (userMap == null) {
            return ResultUtil.resultMap("请重新登录", ResultCode.RESUTL_CODE_FAILURE.code(), null);
        }
        inputMap.put("signAcctNo", reqBody.get("acctNo"));//签约账号     
		inputMap.put("globalType", userMap.get("globalType"));//证件类型
		inputMap.put("globalId", userMap.get("globalId"));//证件号码
		inputMap.put("clientName", userMap.get("userName"));//客户名称
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); //客户风险等级查询结果
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
		    log.info("理财签约接口响应"+JSONObject.fromObject(result));
			return result;
		}
		 JSONObject dataJson = JSONObject.fromObject(result.get("data"));
		 dataJson.remove("head");
		 result.put("data", dataJson);
		log.info("理财签约接口响应"+JSONObject.fromObject(result));
		
		return result;
	}
	
	/**
	 * 理财签约查询 
	 * 
	 * @param reqBody
	 * @return
	 */
	@Override
	public Map<String, Object> querySign(Map<String, String> reqBody) {
		log.info("理财签约查询请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		//调用核心计价因子报文头入参
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.FINANCE_QUERYSIGN.getServiceCode(), TransCodeEnum.FINANCE_QUERYSIGN.getServiceSence(), 
				"", SysConstans.ABP_CODE, "108030");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		inputMap.put("sysHead", reqSysHead);
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); //理财签约查询结果
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
		    log.info("理财签约查询响应"+JSONObject.fromObject(result));
			return result;
		}
		log.info("理财签约查询响应"+JSONObject.fromObject(result));
		return result;
	}
	
	/**
	 * 
	 * 热门理财查询
	 * @param reqBody
	 * @return
	 */
	@Override
	public Map<String, Object> getFinanceHot(Map<String, String> reqBody) {
		log.info("热门理财查询请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		//调用核心计价因子报文头入参
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.FINANCE_PRODUCTALL.getServiceCode(), TransCodeEnum.FINANCE_PRODUCTALL.getServiceSence(), 
				"0110", SysConstans.ABP_CODE, "");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		//调用核心计价因子入参 
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("taCode", reqBody.get("taCode"));        //TA代码
		inputMap.put("productCode", reqBody.get("productCode")); //产品编号
		inputMap.put("productStatus", reqBody.get("productStatus"));        //产品状态
		inputMap.put("productRiskLevel", reqBody.get("productRiskLevel")); //产品风险等级
		inputMap.put("productManager", reqBody.get("productManager"));        //产品经理工号
		inputMap.put("prodProperty", reqBody.get("prodProperty")); //产品属性
		inputMap.put("queryType", reqBody.get("queryType"));        //查询类型
		inputMap.put("templateCode", reqBody.get("templateCode")); //模板编号
		inputMap.put("raiseStartDate", reqBody.get("raiseStartDate"));        //募资起始日期
		inputMap.put("raiseEndDate", reqBody.get("raiseEndDate")); //募资终止日期
		
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); // 理财产品清单列表查询结果
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
		    log.info("理财产品清单列表查询响应"+JSONObject.fromObject(result));
			return result;
		}
	   JSONObject dataJson = JSONObject.fromObject(result.get("data"));
		//返回的是全部产品列表
	    JSONArray productListJson = (JSONArray) dataJson.get("queryResultArray");	
	   //筛选需要字段
	    JSONArray productListJsonNew = new JSONArray();
	  
	    for(int i=0;i<productListJson.size();i++) {
	    	JSONObject productOne =new JSONObject();
	    	productOne.put("productCode", productListJson.getJSONObject(i).get("productCode"));  //产品编号
	    	productOne.put("productName", productListJson.getJSONObject(i).get("productName"));  //产品名称
	    	productOne.put("productType", productListJson.getJSONObject(i).get("productType"));  //产品类型
	    	productOne.put("productStatus", productListJson.getJSONObject(i).get("productStatus"));  //产品状态
	    	productOne.put("productStatusName", productListJson.getJSONObject(i).get("productStatusName")); //产品状态名称
	    	productOne.put("productRiskLevel", productListJson.getJSONObject(i).get("productRiskLevel")); //产品风险等级
	    	productOne.put("productRiskLevelName", productListJson.getJSONObject(i).get("productRiskLevelName")); //产品风险等级名称
	    	productOne.put("prodProperty", productListJson.getJSONObject(i).get("prodProperty")); //产品属性
	    	productOne.put("prodPropertyName", productListJson.getJSONObject(i).get("prodPropertyName")); //产品属性名称
	    	productOne.put("expectYieldS", productListJson.getJSONObject(i).get("expectYieldS")); //预期收益率(字符型的)
	    	productOne.put("cycleDays", productListJson.getJSONObject(i).get("cycleDays")); //周期天数
	    	productOne.put("pFirMinAmt", productListJson.getJSONObject(i).get("pFirMinAmt"));  //个人首次最低投资金额
	    	productOne.put("curAvlLmt", productListJson.getJSONObject(i).get("curAvlLmt")); //当前可用额度
	    	productOne.put("raiseAmtMax", productListJson.getJSONObject(i).get("raiseAmtMax"));  //最高募集金额
	    	productOne.put("endDate", productListJson.getJSONObject(i).get("endDate"));  //募集结束日期
	    	productOne.put("templateCode", productListJson.getJSONObject(i).get("templateCode"));  //募集结束日期
	    	//挡板数据 数据格式为科学计数法+++++++++++++++++++**********************************
	    	Double curAvlLmt =(Double)productListJson.getJSONObject(i).get("curAvlLmt");
	    	Double raiseAmtMax =(Double)productListJson.getJSONObject(i).get("raiseAmtMax");
	    	BigDecimal curAvlLmtB=new BigDecimal(curAvlLmt);
	    	BigDecimal raiseAmtMaxB=new BigDecimal(raiseAmtMax);
	    	productOne.put("curAvlLmt",curAvlLmtB.toPlainString());
	    	productOne.put("raiseAmtMax", raiseAmtMaxB.toPlainString());
	    	//挡板数据 数据格为科学计数法++++++++++++++++++++**********************************
	    	productListJsonNew.add(productOne);
	    }
		//三款产品的集合 
	    JSONArray steadyInJson = new JSONArray();
	    JSONArray weekInJson = new JSONArray();
	    JSONArray dayInJson = new JSONArray();
	    for(int i=0;i<productListJsonNew.size();i++) {
	    	if(productListJsonNew.getJSONObject(i).get("templateCode").equals("1102")) { //稳进账
	    		steadyInJson.add(productListJsonNew.getJSONObject(i));
	    	}else if(productListJsonNew.getJSONObject(i).get("templateCode").equals("1401")) {//周周盈
	    		weekInJson.add(productListJsonNew.getJSONObject(i));
	    	}else {//日日盈
	    		dayInJson.add(productListJsonNew.getJSONObject(i));
	    	}
	    }
	    //热门理财产品的列表 三款产品 三条数据
		JSONArray financeHotJson = new JSONArray();
		
		
		JSONObject obj1=getDate(steadyInJson);	
		JSONObject obj2=getDate(weekInJson);
		JSONObject obj3=getDate(dayInJson);
		if(obj1!=null) {
			financeHotJson.add(obj1);
		}
		if(obj2!=null) {
			financeHotJson.add(obj2);
		}
		if(obj3!=null) {
			financeHotJson.add(obj3);
		}
		
		dataJson.remove("queryResultArray");
		dataJson.put("allFinanceArray",financeHotJson);
		dataJson.remove("head");
		result.remove("data");
		result.put("data", dataJson);	
		log.info("热门理财查询响应"+JSONObject.fromObject(result));
		return result;
		
	}
	
	private  JSONObject getDate(JSONArray steadyInJson) {
		 //当前日期
		String today ="";
		Date dt =new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
		today=sdf.format(dt);
		if(steadyInJson.size()!=0) {//稳进账
			String endDate0=DateUtils.getdatefor_yyyy_MM_dd(steadyInJson.getJSONObject(0).getString("endDate"));
			Long min =Math.abs(DateUtils.getQuotByDays(today,endDate0));
			int index =0;
			for(int i=0;i<steadyInJson.size();i++) { 
		    	if(StringUtils.isNotBlank(steadyInJson.getJSONObject(i).getString("endDate"))) {
		    		 String endDate=DateUtils.getdatefor_yyyy_MM_dd(steadyInJson.getJSONObject(i).getString("endDate"));
		    		 Long days =Math.abs(DateUtils.getQuotByDays(today,endDate));
		    		 if(min>days) {
		    			 min=days;
		    			 index=i;
		    		 }
		    	}
		    }
			return steadyInJson.getJSONObject(index);
		}else {
			return null;
		}
		
	}
	
   /* //字符串的日期格式的计算 
	public  int daysBetween(String smdate,String bdate) {
		if(StringUtils.isNotBlank(smdate)&&StringUtils.isNotBlank(bdate)) {
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
		    Calendar cal = Calendar.getInstance();  
		    long time1 = cal.getTimeInMillis();    
		    try {
			    cal.setTime(sdf.parse(smdate));
			    cal.setTime(sdf.parse(bdate));   
		    } catch (ParseException e) {
			    log.error("时间轴日期计算错误");
			}    
		    long time2 = cal.getTimeInMillis();         
		    long between_days=(time2-time1)/(1000*3600*24);  
		    return Integer.parseInt(String.valueOf(between_days));     
	   }
	   return 0;
	}  */
		 
	
	
	
	
}